{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# RSA Digital Signature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from math import sqrt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def exp_mod(a, b, n):\n",
    "    \"\"\"\n",
    "    a to the power of b, mod n\n",
    "    >>> exp_mod(2, 3, 6)\n",
    "    2\n",
    "    \"\"\"\n",
    "    return (a**b) % n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def gcd(a, b):\n",
    "    \"\"\"\n",
    "    Greatest common divisor\n",
    "    >>> gcd(72, 5)\n",
    "    1\n",
    "    \"\"\"\n",
    "    if b > a:\n",
    "        return gcd(b, a)\n",
    "    if a % b == 0:\n",
    "        return b\n",
    "    return gcd(b, a % b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def gcd_qs(a, b):\n",
    "    \"\"\"\n",
    "    List of q values from gcd(a, b)\n",
    "    >>> gcd(72, 5)\n",
    "    [14, 2]\n",
    "    \"\"\"\n",
    "    if b > a:\n",
    "        a, b = b, a\n",
    "    assert a > b\n",
    "    assert gcd(a, b) == 1\n",
    "    \n",
    "    rem = None\n",
    "    while rem != 1:\n",
    "        q = a // b       # 72 // 5 = 14        5 // 2 = 2\n",
    "        rem = a - q * b  # 72 - 5*14 = 2       5 - 2*2 = 1\n",
    "        a = b            # a is now 5          a is now 2\n",
    "        b = rem          # b is now 2          b is now 1\n",
    "        yield q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def mod_inv(a, n):\n",
    "    \"\"\" \n",
    "    Modular inverse\n",
    "    >>> inv_mod(72, 5)\n",
    "    29\n",
    "    \"\"\"\n",
    "    t0, t1 = 0, 1\n",
    "    for q in gcd_qs(a, n):\n",
    "        t = t0 - q * t1\n",
    "        t0, t1 = t1, t  # move them all back one space\n",
    "    return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def is_prime(n):\n",
    "    \"\"\"\n",
    "    Check whether a number is prime\n",
    "    >>> is_prime(5)\n",
    "    True\n",
    "    \"\"\"\n",
    "    return all(n % i != 0 \n",
    "               for i in range(2, int(sqrt(n))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 1) Generate Keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from random import shuffle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# pick two big prime numbers\n",
    "p = 101\n",
    "q = 499\n",
    "\n",
    "assert is_prime(p)\n",
    "assert is_prime(q)\n",
    "\n",
    "n = p * q  # first part of public key\n",
    "phi_n = (p-1)*(q-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v = 167\n"
     ]
    }
   ],
   "source": [
    "# pick v, second part of public key\n",
    "valid_range = list(range(p+1, q))\n",
    "shuffle(valid_range)  # shuffles in place\n",
    "\n",
    "for v in valid_range:       # search for a prime in (p, q)\n",
    "    if not is_prime(v):     # need v to be prime\n",
    "        continue\n",
    "    if gcd(v, phi_n) == 1:  # found one that fits\n",
    "        break\n",
    "        \n",
    "print(f'v = {v}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 2) Sign Message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# compute s\n",
    "s = mod_inv(v, phi_n)  # private key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "signed message = 16437\n"
     ]
    }
   ],
   "source": [
    "# desired message to send\n",
    "msg = 321 \n",
    "assert 1 < msg < n\n",
    "\n",
    "signed_msg = exp_mod(msg, s, n)\n",
    "print(f'signed message = {signed_msg}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 3) Verify Authenticity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "decrypted message = 321\n",
      "same as original message: True\n"
     ]
    }
   ],
   "source": [
    "decrypted = exp_mod(signed_msg, v, n)\n",
    "print(f'decrypted message = {decrypted}')\n",
    "print(f'same as original message: {decrypted == msg}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
